package zj.com.fragments;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import zj.com.activities.DqrEditActivity;
import zj.com.adapter.DqrACompanyAdapter;
import zj.com.app.ZjApplication;
import zj.com.interfaze.CallData;
import zj.com.model.DqrAccompany;
import zj.com.model.JcdInfoDB;
import zj.com.model.Org;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/4.
 * 检查项
 */
public class DqrRightFragment extends Fragment implements View.OnClickListener,CallData {

    public RelativeLayout error_rl;
    public ListView listView;
    public List<DqrAccompany> list;
    public DqrACompanyAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dqr, container, false);
        initView(view);
        return view;
    }


    public void initView(View view){

        list = new ArrayList<>();
        adapter = new DqrACompanyAdapter(list,getActivity());
        adapter.setCallback(this);
        error_rl = (RelativeLayout) view.findViewById(R.id.error_rl);
        listView = (ListView) view.findViewById(R.id.listView);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), DqrEditActivity.class);
                DqrAccompany acc = list.get(position);
                JcdInfoDB jcd = new JcdInfoDB();
                jcd.setServerInpsectId(acc.getInspectionID());
                ZjApplication.dqrJcdInfo = jcd;
                ZjApplication.isAcc = 0;
                intent.putExtra("accId", acc.getAccompanyID() + "");
                startActivity(intent);
            }
        });


    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if(isVisibleToUser){
        }
    }

    @Override
    public void onResume() {
        super.onResume();            obtainData();

    }

    public void obtainData(){

        NetManager.getInstance(getActivity()).GetAccompanyInspectionList(new OnResponseListener<List<DqrAccompany>>() {
            @Override
            public void onComplete(boolean isOK, List<DqrAccompany> result, int errorCode, String errorMsg) {

                if(isOK){

                    if(result != null && result.size() > 0){

                        list = result;
                        error_rl.setVisibility(View.GONE);
                    }else{
                        error_rl.setVisibility(View.VISIBLE);

                        list.clear();
                    }

                    adapter.setDataChange(list);
                }else{
                    Toast.makeText(getActivity(), "获取失败", Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {

                Toast.makeText(getActivity(), "获取失败", Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public void onClick(View v) {

    }

    public ProgressDialog dialog;
    @Override
    public void call(Object obj, String state) {

        dialog = DialogUtil.createProgressDialog(getActivity(),"提示","确认中");
        LogUtils.getInstance().error("=====state====="+state);
        NetManager.getInstance(getActivity()).ConfirmAccompany(state, new OnResponseListener<String>() {
            @Override
            public void onComplete(boolean isOK, String result, int errorCode, String errorMsg) {

                if(isOK){

                    Toast.makeText(getActivity(),"确认成功",Toast.LENGTH_SHORT).show();
                    obtainData();
                }
                dialog.dismiss();
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                dialog.dismiss();
            }
        });
    }
}
